<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>11.移动动画的闭包使用</title>
  <style>
    button {
      position: absolute;
    }
  </style>
</head>

<body>
  <button message="后盾人">houdunren</button>
  <button message="hdcms">hdcms</button>
  <script>

    let btns = document.querySelectorAll("button");
    btns.forEach(function (item) {
      item.addEventListener("click", function () {
        // 这样写点击两次会抖动，需要用闭包来解决
        // 函数执行一次会开辟新的环境，所以每次点击 left 都 =1
        let left = 1;
        console.log(left);
        // 会自动寻找父作用域内的item变量
        setInterval(function () {
          item.style.left = left++ + "px";
        }, 5);
      });
    });
  </script>
</body>

</html>